home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3467 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: news2.aimnet.com!usenet
  2. From: gander@aimnet.com (Geoffrey Anderson)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Processor Fault!!??
  5. Date: Mon, 29 Jan 1996 07:42:17 GMT
  6. Organization: Aimnet Information Services
  7. Message-ID: <310c792e.4231971@news2.aimnet.com>
  8. References: <Pine.A32.3.91j.960128010358.43648E-100000@homer23.u.washington.edu>
  9. NNTP-Posting-Host: dial-milp-28.iway.aimnet.com
  10. X-Newsreader: Forte Agent .99c/16.141
  11.  
  12. "Q. Sun" <quanger@u.washington.edu> wrote:
  13.  
  14. >
  15. >I was doing some examples off a text we are using and I am having 
  16. >problems with it. The chapter is on arrays and I type in and ran the 
  17. >programs in the text. When I ran it using Turbo C++ 3.1 it gave a nasty 
  18. >General Protection Fault and stating it's the processor's fault! It also 
  19. >said things like "0x211F:0x00BE Processor Fault." In Windows 95 it total 
  20. >crashed! But in Windows for Workgroups 3.11, it recovered a 
  21. >couple of runs until it eventually kicked me out to DOS.
  22. >
  23. >I'm running a Pentium 120 with 16megs of RAM. Do I have one of those old 
  24. >faulty Pentiums? My system is about one week new.
  25. >
  26. >The code example is EXACTLY as follows:
  27. >
  28. >/* Program to convert a postivie interger to another base */
  29. >#include <stdio.h>
  30. >main ()
  31. >{
  32. >    char base_digits[16] =    { '0', '1', '2', '3', '4', '5', '6', '7',
  33. >                '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  34. >    int converted_number[64];
  35. >    int number_to_convert;
  36.              ^^^ should be long since you scan for a long signed
  37. integer in your scanf...
  38. >    int next_digit, base, index = 0;
  39. >
  40. >    /* get the number and the base */
  41. >
  42. >    printf ("Number to be converted? ");
  43. >    scanf  ("%ld", &number_to_convert);
  44. >    printf ("Base? ");
  45. >    scanf  ("%i", &base);
  46. >
  47. >    /* convert to the indicated base */
  48. >
  49. >    do
  50. >    {
  51. >        converted_number[index] = number_to_convert % base;
  52. >        ++index;
  53. >        number_to_convert = number_to_convert / base;
  54. >    }
  55. >    while ( number_to_convert != 0);
  56. >
  57.     Careful examination says that you have the index incremented
  58. so you are actually pointing to an un-known array element.  When you
  59. look up the digit, you quite probably have a wrong pointer and BANG it
  60. goes crash!
  61.  
  62. Try adding a index-- before the for loop....
  63.  
  64. >
  65.     /* display the results in reverse order */
  66. >
  67. >    printf ("Converted number = ");
  68. >
  69. >    for ( index; index >= 0; --index )
  70. >    {
  71. >        next_digit = converted_number[index];
  72. >        printf("%c", base_digits[next_digit]);
  73. >    }
  74. >
  75. >    printf ("\n");
  76. >
  77. >} 
  78. >
  79. >/* Code ends here */
  80. >
  81. >
  82. >
  83.  
  84.